﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HeliTracking.Properties;
using MapLib;
using MapLib.Examples;
using MapWinGIS;

namespace HeliTracking
{
    public partial class frmMain : Form
    {
        public ucMap objMapControl = new ucMap();
        public ucToolStrip objToolStripControl;
        public AxMapWinGIS.AxMap axMap1;
        public frmMain()
        {
            InitializeComponent();

            axMap1 = objMapControl.AxMap1;
            objToolStripControl = new ucToolStrip(axMap1);
            objToolStripControl.Dock = DockStyle.Top;
            this.Controls.Add(objToolStripControl);

            this.Controls.Add(this.menuStrip1);
            objMapControl.Dock = DockStyle.Fill;
            this.Controls.Add(objMapControl);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            MapWinGIS.Shapefile sfworld1 = new ShapefileClass();
            sfworld1.Open(@"e:\ShapeFiles\Areas.shp");
            int intHandler1 = axMap1.AddLayer(sfworld1, true);

            //Set Filling color of the ploygon shapefile
            axMap1.set_ShapeLayerFillColor(intHandler1,
                (UInt32)(System.Drawing.ColorTranslator.ToOle
                        (System.Drawing.Color.Gray)));

            //Set the line color
            axMap1.set_ShapeLayerLineColor(intHandler1,
                (UInt32)(System.Drawing.ColorTranslator.ToOle
                        (System.Drawing.Color.DarkGoldenrod)));
            //Set the line width
            //axMap1.set_ShapeLayerLineWidth(intHandler1, 5);
            axMap1.CtlBackColor = Color.Gray;

            MapWinGIS.Shapefile sfworld2 = new ShapefileClass();
            sfworld2.Open(@"e:\ShapeFiles\Lines.shp");
            int intHandler2 = axMap1.AddLayer(sfworld2, true);
            //Set Filling color of the ploygon shapefile
            axMap1.set_ShapeLayerFillColor(intHandler2,
                (UInt32)(System.Drawing.ColorTranslator.ToOle
                        (System.Drawing.Color.Gray)));

            //Set the line color
            axMap1.set_ShapeLayerLineColor(intHandler2,
                (UInt32)(System.Drawing.ColorTranslator.ToOle
                        (System.Drawing.Color.DarkGoldenrod)));
        }

        private void tọaĐộ9x9ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            objMapControl.ShowCoordinate9x9 = true;
            objMapControl.Refresh();
        }

        private void Draw1(string dataPath)
        {
            string[] filenames = new string[1];
            filenames[0] = dataPath + "road.shp";
            Extents extents = null;

            axMap1.LockWindow(tkLockMode.lmLock);
            try
            {
                for (int n = 0; n < filenames.Length; n++)
                {
                    Shapefile sf = new Shapefile();
                    if (sf.Open(filenames[n], null))
                    {
                        if (extents == null)
                            extents = sf.Extents;   // the extents of the fist shapefile wil be used to setup display

                        int drawHandle = axMap1.NewDrawing(tkDrawReferenceList.dlSpatiallyReferencedList);
                        for (int i = 0; i < sf.NumShapes; i++)
                        {
                            Shape shp = sf.get_Shape(i);

                            if (shp.ShapeType == ShpfileType.SHP_POINT)
                            {
                                double x = 0.0;
                                double y = 0.0;
                                shp.get_XY(0, ref x, ref y);
                                axMap1.DrawPointEx(drawHandle, x, y, 5, 0);
                            }
                            else
                            {
                                for (int p = 0; p < shp.NumParts; p++)
                                {
                                    int initIndex = shp.get_Part(p);
                                    int numPoints = shp.get_EndOfPart(p) - shp.get_Part(p) + 1;
                                    if (numPoints > 0)
                                    {
                                        double[] x = new double[numPoints];
                                        double[] y = new double[numPoints];

                                        for (int j = 0; j < numPoints; j++)
                                        {
                                            shp.get_XY(j + initIndex, ref x[j], ref y[j]);
                                        }

                                        object xObj = x as object;
                                        object yObj = y as object;
                                        bool drawFill = shp.ShapeType == ShpfileType.SHP_POLYGON;
                                        uint color = sf.ShapefileType == ShpfileType.SHP_POLYGON ? sf.DefaultDrawingOptions.FillColor :
                                                                                                   sf.DefaultDrawingOptions.LineColor;
                                        axMap1.DrawPolygonEx(drawHandle, ref xObj, ref yObj, numPoints, color, drawFill);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            finally
            {
                axMap1.Extents = extents;
                axMap1.LockWindow(tkLockMode.lmUnlock);
                axMap1.Redraw();
            }
        }

        public void CreateShapeFile(string dataPath)
        {
            //Create new shapefile
            Shapefile myShapefile = new Shapefile();
            //Define the path of the new shapefile and geometry type
            myShapefile.CreateNew(dataPath + "drawing.shp", ShpfileType.SHP_POINT);
        }
        private void drawToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string dataPath = @"Maps\";
            Draw1(dataPath);
            //axMap1.PixelToProj();
            //MapExamples exam = new MapExamples();
            //exam.axMap1 = axMap1;
            //exam.Tracking(dataPath);
        }
    }
}
